package iot;

import io.netty.channel.ChannelFuture;
import netty.session.SessionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

/**
 * Created by dave on 18-7-20 下午11:07.
 */
@SpringBootApplication
public class Application implements ApplicationListener<ContextRefreshedEvent> {
    private static Logger L = LoggerFactory.getLogger(Application.class);
    @Value("${tcp.port}")
    private int port;

    @Autowired SessionManager sessionManager;
    @Autowired MessageProcessor messageProcessor;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        TcpServer server = new TcpServer(port, sessionManager, messageProcessor);
        ChannelFuture f = server.start();
        f.addListener(future -> {
            if (future.isSuccess()) {
                L.info("服务启动成功");
            } else {
                L.error("服务启动失败", future.cause());
            }
        });
    }
}
